package com.moody.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.ui.Model;

import com.moody.hibernate.domain.ModelHis;
import com.moody.hibernate.domain.ModelInfo;

/**
 	* A data access object (DAO) providing persistence and search support for ModelInfo entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.moody.hibernate.domain.ModelInfo
  * @author MyEclipse Persistence Tools 
 */

public class ModelInfoDAO extends HibernateDaoSupport  {
	     private static final Logger log = LoggerFactory.getLogger(ModelInfoDAO.class);
		//property constants
	public static final String MODEL_ID = "modelId";
	public static final String DATA_FLAG = "dataFlag";
	public static final String DESCRIBE1 = "describe1";
	public static final String DESCRIBE2 = "describe2";
	public static final String DESCRIBE3 = "describe3";
	public static final String ITEM_NO = "itemNo";
	public static final String SELECT_ITEM = "selectItem";
	public static final String ITEM_DESCRIBE = "itemDescribe";
	public static final String ITEM_VALUE = "itemValue";
	public static final String ITEM_WEIGHT = "itemWeight";
	public static final String ITEM_SCORE = "itemScore";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String UPDATE_USER_ID = "updateUserId";
	public static final String UPDATE_ORG_ID = "updateOrgId";
	public static final String UPDATE_DATE = "updateDate";



	protected void initDao() {
		//do nothing
	}
    
    public void save(ModelInfo transientInstance) {
        log.debug("saving ModelInfo instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(ModelInfo persistentInstance) {
        log.debug("deleting ModelInfo instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public ModelInfo findById( java.lang.String id) {
        log.debug("getting ModelInfo instance with id: " + id);
        try {
            ModelInfo instance = (ModelInfo) getHibernateTemplate()
                    .get("com.moody.hibernate.domain.ModelInfo", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(ModelInfo instance) {
        log.debug("finding ModelInfo instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding ModelInfo instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from ModelInfo as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByModelId(Object modelId
	) {
		return findByProperty(MODEL_ID, modelId
		);
	}
	
	public List findByDataFlag(Object dataFlag
	) {
		return findByProperty(DATA_FLAG, dataFlag
		);
	}
	
	public List findByDescribe1(Object describe1
	) {
		return findByProperty(DESCRIBE1, describe1
		);
	}
	
	public List findByDescribe2(Object describe2
	) {
		return findByProperty(DESCRIBE2, describe2
		);
	}
	
	public List findByDescribe3(Object describe3
	) {
		return findByProperty(DESCRIBE3, describe3
		);
	}
	
	public List findByItemNo(Object itemNo
	) {
		return findByProperty(ITEM_NO, itemNo
		);
	}
	
	public List findBySelectItem(Object selectItem
	) {
		return findByProperty(SELECT_ITEM, selectItem
		);
	}
	
	public List findByItemDescribe(Object itemDescribe
	) {
		return findByProperty(ITEM_DESCRIBE, itemDescribe
		);
	}
	
	public List findByItemValue(Object itemValue
	) {
		return findByProperty(ITEM_VALUE, itemValue
		);
	}
	
	public List findByItemWeight(Object itemWeight
	) {
		return findByProperty(ITEM_WEIGHT, itemWeight
		);
	}
	
	public List findByItemScore(Object itemScore
	) {
		return findByProperty(ITEM_SCORE, itemScore
		);
	}
	
	public List findByInputUserId(Object inputUserId
	) {
		return findByProperty(INPUT_USER_ID, inputUserId
		);
	}
	
	public List findByInputOrgId(Object inputOrgId
	) {
		return findByProperty(INPUT_ORG_ID, inputOrgId
		);
	}
	
	public List findByInputDate(Object inputDate
	) {
		return findByProperty(INPUT_DATE, inputDate
		);
	}
	
	public List findByUpdateUserId(Object updateUserId
	) {
		return findByProperty(UPDATE_USER_ID, updateUserId
		);
	}
	
	public List findByUpdateOrgId(Object updateOrgId
	) {
		return findByProperty(UPDATE_ORG_ID, updateOrgId
		);
	}
	
	public List findByUpdateDate(Object updateDate
	) {
		return findByProperty(UPDATE_DATE, updateDate
		);
	}
	

	public List findAll() {
		log.debug("finding all ModelInfo instances");
		try {
			String queryString = "from ModelInfo";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public ModelInfo merge(ModelInfo detachedInstance) {
        log.debug("merging ModelInfo instance");
        try {
            ModelInfo result = (ModelInfo) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(ModelInfo instance) {
        log.debug("attaching dirty ModelInfo instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(ModelInfo instance) {
        log.debug("attaching clean ModelInfo instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static ModelInfoDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (ModelInfoDAO) ctx.getBean("ModelInfoDAO");
	}

	public List<ModelInfo> findModelInfoByModelIdAndDataFlag(String modelId,String dataFlag) {
		String queryString = "from ModelInfo as model where model.modelId = ? and model.dataFlag = ?";
		List<ModelInfo> list = getHibernateTemplate().find(queryString, modelId, dataFlag);
		
		log.info("modelId: "+modelId+" dataFlag"+ dataFlag +"因素: "+"共"+list.size()+"项");
		
		return list;
	}

	public void insertModelInfo(List<ModelInfo> list) {
		try{
			getHibernateTemplate().saveOrUpdateAll(list);
		}catch (Exception e){
			log.error("insert error", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<ModelInfo> findByModelIdAndDescribe2AndDataFlagAndModifiedField(
			String modelId,String describe2,  String dataFlag, String field, String value) {
		List<ModelInfo> list = new ArrayList<ModelInfo>();
		
		String queryString = "from ModelInfo as model where model.modelId = ? and model.describe2 = ? and model.dataFlag = ? and model." + field +" = ?";
		if(field.equals("itemValue") || field.equals("itemWeight")){
			list = getHibernateTemplate().find(queryString, modelId, describe2, dataFlag, Double.valueOf(value));
		}else{
			list = getHibernateTemplate().find(queryString, modelId, describe2 ,dataFlag, value);
		}
		log.info("modelId: "+modelId+" dataFlag"+ dataFlag +"因素: "+"共"+list.size()+"项");
		
		return list;
	}

	public void update(ModelInfo transientInstance) {
        log.debug("updating ModelInfo instance");
        try {
            getHibernateTemplate().update(transientInstance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

	@SuppressWarnings("unchecked")
	public List<ModelInfo> findByModelIdAndDescribe2AndDataFlagAndModifiedField(
			String modelId, String describe2, String dataFlag, String field) {
		List<ModelInfo> list = new ArrayList<ModelInfo>();
		
		String queryString = "from ModelInfo as model where model.modelId = ? and model.describe2 = ? and model.dataFlag = ?";
		if(field.equals("itemValue") || field.equals("itemWeight")){
			list = getHibernateTemplate().find(queryString, modelId, describe2, dataFlag);
		}else{
			list = getHibernateTemplate().find(queryString, modelId, describe2 ,dataFlag);
		}
		log.info("modelId: "+modelId+" dataFlag"+ dataFlag +"因素: "+"共"+list.size()+"项");
		
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<ModelInfo> findModelInfoByModelId(String modelId) {	
		List<ModelInfo> list = new ArrayList<ModelInfo>();
		
		String queryString = "from ModelInfo as model where model.modelId = ?";
		list = getHibernateTemplate().find(queryString, modelId);
		log.info("返回"+list.size()+"条记录");
		
		return list;
	}

	public void deleteModelInfo(List<ModelInfo> nowList) {
		log.debug("deleting ModelInfo instance list");
        try {
            getHibernateTemplate().deleteAll(nowList);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
		
	}
}